package nid.gallery.view 
{
	import caurina.transitions.Tweener;
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;
	import nid.gallery.IView;
	
	/**
	 * ...
	 * @author Nidin P Vinayakan
	 */
	public class ImageView extends Sprite implements IView
	{
		private var image:Bitmap; // Loaded Bitmap image data 
		private var imageHolder:Sprite; //bitmap image holder sprite
		private var bg:Sprite; // background graphics sprite
		private var loader:Loader;
		private var info:InfoView;
		
		public function ImageView() 
		{
			/**
			 * create background graphics 
			 */
			bg = new Sprite();
			bg.graphics.beginFill(0xffffff,0.75);
			bg.graphics.drawRect(0, 0, 100, 100);
			bg.graphics.endFill();
			addChild(bg);			
			
			imageHolder = new Sprite(); //create image holder
			addChild(imageHolder);
			
			info = new InfoView();
			info.alpha = 0;
			info.x = 10;
			info.y = bg.height - info.height - 10;
			addChild(info);
			
			addEventListener(Event.ADDED_TO_STAGE, init); //when this object added to stage call init function
			
			loader = new Loader();
		}
		
		private function init(e:Event):void 
		{
			//remove event listener
			removeEventListener(Event.ADDED_TO_STAGE, init);
			/**
			 * set initial position of the big image viewer
			 */
			this.x = stage.stageWidth / 2 - bg.width / 2;
			this.y = stage.stageHeight / 2 - bg.height / 2;
			
			addEventListener(MouseEvent.ROLL_OVER, showInfo);
			addEventListener(MouseEvent.ROLL_OUT, showInfo);
		}
		
		private function showInfo(e:MouseEvent):void 
		{
			if (e.type == "rollOver")
			{
				Tweener.addTween(info, { alpha:1, time:0.5, transition:"easeOutExpo" } );
			}
			else if (e.type == "rollOut")
			{
				Tweener.addTween(info, { alpha:0, time:0.5, transition:"easeOutExpo" } );
			}
		}
		public function push(media:XML):void
		{
			/**
			 * push image to view
			 */
			loader.unload();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, forwardIOError);
			loader.load(new URLRequest(media.@src));
		}
		
		private function onComplete(e:Event):void 
		{
			/**
			 * if image is already loaded(previous image) create function for remove old image 
			 */
			if (image != null && imageHolder.contains(image))//check if the image is loaded
			{
				var temp_image:Bitmap = image; //save old image to temperary variable
				
				/**
				 * add motion tween animation for fade out old image 
				 */
				Tweener.addTween(temp_image, { //target
					alpha:0, //animation property
					time:0.25, //animation time
					transition:"easeOutQuad", // transition type constant
					onComplete:removeOldImage } ); //on animation complete call "removeOldImage" function 
				
				function removeOldImage():void
				{
					/**
					 * after 150 millisecond delay call function for remove old image from image holder
					 */
					setTimeout(function():void
					{
						imageHolder.removeChild(temp_image); //remove image from display list 
						temp_image = null; //delete image data (system will free the memory used to store the image)
					},150);//delay time in milliseond 
				}
			}
			
			image = Bitmap(e.target.content);
			image.alpha = 0;
			image.x = 10;
			image.y = 10;
			
			align();
			
			imageHolder.addChild(image); // add image to image holder
			
			
		}
		public function clear():void
		{
			
		}
		public function align():void
		{
			var new_x:int = stage.stageWidth / 2 - (image.width +20) / 2;
			var new_y:int = stage.stageHeight / 2 - (image.height + 20) / 2;
			
			info.width = image.height;
			info.y = (image.height + 20) - (info.height + 10);
			
			/**
			 * set x and y of image viewer (big image container) with animation (motion tween)
			 */
			Tweener.addTween(this, { 
				x:new_x, 
				y:new_y, 
				time:0.5, 
				transition:"easeOutQuad" } );
			/**
			 * set with and height of background with animation (motion tween)
			 */
			Tweener.addTween(bg, {
				width:(image.width + 20), 
				height:(image.height + 20), 
				time:0.75, 
				transition:"easeOutQuad" } );
			/**
			 * set alpha(visiblity) to 1 (maximum) with animation (motion tween)
			 */
			Tweener.addTween(image, { 
				alpha:1, 
				time:0.5, 
				transition:"easeOutQuad",
				delay:0.5 } );
		}
		private function forwardIOError(e:IOErrorEvent):void 
		{
			dispatchEvent(e);
			trace('io error');
		}
		
	}

}